<?php

require_once dirname(__FILE__) . '/../lib/moduloGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/moduloGeneratorHelper.class.php';

/**
 * modulo actions.
 *
 * @package    sistema_itec_new
 * @subpackage modulo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class moduloActions extends autoModuloActions {

    function executeAjaxNew(sfWebRequest $request) {
        $form = new moduloForm();
        $module = $this->getModuleName();

        if ($request->isMethod(sfRequest::POST)) {
            $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
            if ($form->isValid()) {
                $object = $form->save();

                return $this->renderText('SUCCESS. [id:' . $object->getid() . '][nombre:' . $object . '][model:' . $module . ']');
            }
        }

        $title = 'Nuevo ' . ucwords(str_replace('_', ' ', $module));
        $backend_route = sfConfig::get('app_backend_route');
        $html = <<<EOF
            <form method="post" action="/$backend_route/$module/AjaxNew">
                <h2>$title</h2>
                <div class="box">
                    $form
                </div>
EOF;
        return $this->renderText($html);
    }

    public function executePrintList(sfWebRequest $request) {
        $this->modulo = Doctrine::getTable('Modulo')->findOneById($request->getParameter('id'));
        $this->alumnos_inscriptos = Doctrine::getTable('Modulo')->getAlumnosInscriptos($this->modulo->getId());
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
        $filename = $this->modulo->getNombre();
        header("Content-Type: application/vnd.ms-excel ; charset=UTF-8");
        header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
        return $this->renderPartial('printList');
    }

    public function executeShowRegistration(sfWebRequest $request) {
        $this->modulo = Doctrine::getTable('Modulo')->findOneById($request->getParameter('id'));
        $this->alumnos_inscriptos = Doctrine::getTable('Modulo')->getAlumnosInscriptos($this->modulo->getId());
    }

    public function executeBatchUnregistration(sfWebRequest $request) {
        $modulo_id = $request->getPostParameter('modulo_id');
        $alumnos = $request->getParameter('alumno_delete');
        if (!isset($alumnos)) {
            $this->getuser()->setFlash('error', 'No seleccionó ningun alumno.');
            $this->redirect('modulo/showRegistration?id=' . $modulo_id);
        }
        $success = 0;
        $errors = 0;
        foreach ($alumnos as $alumno_id => $alumno) {
            $result = Doctrine::getTable('Alumno')->deleteRegistration($alumno_id, $modulo_id);
            if ($result)
                $success++;
            if (!$result)
                $errors++;
        }
        if ($success == 0) {
            $this->getuser()->setFlash('error', 'Se ha producido un error al procesar los datos.');
        } elseif ($success == 1) {
            if ($errors == 1) {
                $this->getuser()->setFlash('notice', '1 registro eliminado. 1 error.');
            } else {
                $this->getuser()->setFlash('notice', '1 registro eliminado. '.$errors.' errores');
            }
        } else {
            if ($errors == 1) {
                $this->getuser()->setFlash('notice', $success.' registros eliminados. 1 error.');
            } else {
                $this->getuser()->setFlash('notice', $success.' registros eliminados. '.$errors.' errores');
            }
        }
        $this->redirect('modulo/showRegistration?id=' . $modulo_id);
    }

}
